package com.example.foodisimportant.Network;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;

public class PersonNetworkThread implements Callable<String> {
    private final String name,number,password,campus,college,budget,flavour,strimage,url,rsp;
    public PersonNetworkThread(String name,String number,String password,String campus,String collage,String budget,String flavour,String strimage,String url,String rsp){
        this.name=name;
        this.number=number;
        this.campus=campus;
        this.password=password;
        this.college=collage;
        this.budget=budget;
        this.flavour=flavour;
        this.strimage=strimage;
        this.url=url;
        this.rsp=rsp;
    }

    @Override
    public String call() throws Exception {
        String data;
        try {
            //开启连接
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            //允许输入输出
            connection.setDoInput(true);
            connection.setDoOutput(true);
            if (rsp=="0"){
                //设置请求方法
                connection.setRequestMethod("POST");
                //拼接数据
                data = "userId="+ URLEncoder.encode(number, StandardCharsets.UTF_8.toString())
                        +"&userName="+ URLEncoder.encode(name, StandardCharsets.UTF_8.toString())
                        +"&userCampus="+URLEncoder.encode(campus,StandardCharsets.UTF_8.toString())
                        +"&userFlavour="+URLEncoder.encode(flavour,StandardCharsets.UTF_8.toString())
                        +"&userBudget="+URLEncoder.encode(budget,StandardCharsets.UTF_8.toString())
                        +"&userOpinion="+URLEncoder.encode(college,StandardCharsets.UTF_8.toString())
                        +"&userStrImage="+URLEncoder.encode(strimage,StandardCharsets.UTF_8.toString());
                //写数据（也就是发送数据）
                connection.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
                byte [] bytes = new byte[1024];
                //获取返回的数据
                int len = connection.getInputStream().read(bytes);
                return new String(bytes,0,len,StandardCharsets.UTF_8);
            }else {
                //设置请求方法
                connection.setRequestMethod("GET");
                //拼接数据
                data = "userId="+URLEncoder.encode(number, StandardCharsets.UTF_8.toString())+"&userPwd="+URLEncoder.encode(password, StandardCharsets.UTF_8.toString());
                //写数据（也就是发送数据）
                connection.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
                byte [] bytes = new byte[1024];
                //获取返回的数据
                int len = connection.getInputStream().read(bytes);
                return new String(bytes,0,len,StandardCharsets.UTF_8);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
